package com.qifan.alb2b.action.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qifan.alb2b.action.back.param.RouteQueryParam;
import com.qifan.alb2b.action.common.PageController;
import com.qifan.alb2b.domain.RouteInfoDomain;
import com.qifan.alb2b.service.RouteInfoService;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

@Controller
@RequestMapping("/route")
public class RouteController extends PageController{
	private RouteInfoService routeInfoService;

	public void setRouteInfoService(RouteInfoService routeInfoService) {
		this.routeInfoService = routeInfoService;
	}
	
	/**
	 * 根据城市和团队类型获得所有线路列表
	 * @param request
	 * @param cityId
	 * @param type
	 * @return
	 */
	@RequestMapping("/queryRouteByCity.usr")
	public String queryRouteByCity(HttpServletRequest request, String cityId,int type){
		DataGridModel dgm=new DataGridModel();
		RouteQueryParam rqp=new RouteQueryParam();
		rqp.setOrgCityId(cityId);
		rqp.setStatus(Constants.ROUTE_STATUS_OPEN);
		request.setAttribute("routes",routeInfoService.query(dgm, rqp).getRows());
		request.setAttribute("type", type);
		request.setAttribute("cityId", cityId);
		request.getSession(true).setAttribute("type", type);//把团队type放到session中去，避免传递的麻烦
		return "pages/front/inc/city_route";
	}
	
//	/**
//	 * 获得推荐线路列表
//	 * @param request
//	 * @param type
//	 * @return
//	 */
//	@RequestMapping("/insRoute.usr")
//	public String insRoute(HttpServletRequest request){
//		DataGridModel dgm=new DataGridModel();
//		RouteQueryParam rqp=new RouteQueryParam();
//		rqp.setHot(Constants.INS_YES);
//		request.setAttribute("insRoute",routeInfoService.query(dgm, rqp).getRows());
//		return "pages/front/inc/insRoute";
//	}
	@RequestMapping("/insRoute.usr")
	public String insRoute(HttpServletRequest request,DataGridModel dgm,RouteQueryParam rqp,String countryName){
		super.page(request, dgm);//调用父类的分页逻辑
		rqp.setStatus(Constants.ROUTE_STATUS_OPEN);//只返回开通的线路
		rqp.setHot(Constants.INS_YES);
	//	rqp.setCountryName(countryName);
		Page page= routeInfoService.query(dgm, rqp);
		
		request.setAttribute("page", page);
		request.setAttribute("rqp", rqp);
		return "pages/front/inc/insRoute";
	}
	
	/**
	 * 根据线路ID和团队类型获得线路的标准配套价格和线路详情
	 * @param request
	 * @param routeId
	 * @param groupType
	 * @return
	 */
	@RequestMapping("/queryRoute.usr")
	public String queryRoute(HttpServletRequest request,String routeId,int type){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=null;
		String isBack=request.getParameter("back");
		//对于从上一步返回的操作优先读取session中的routeVo
		if(session.getAttribute(Constants.SESSION_ROUTE_VO)!=null&&isBack!=null&&isBack.equals("0")){
			routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		}else{
			routeVo=routeInfoService.queryRouteVo(routeId, type);
			session.setAttribute(Constants.SESSION_ROUTE_VO, routeVo);//把当前预定的RouteVo放到session中
		}
		request.setAttribute("spt", routeVo.getInitStPriceTable());
		request.setAttribute("route", routeVo.getRoute());
		request.setAttribute("details", routeVo.getDetailVoList());
		return "pages/front/book_route";
	}
	
	@RequestMapping("/queryMoreRoute.usr")
	public String queryMoreRoute(HttpServletRequest request,DataGridModel dgm,RouteQueryParam rqp){
		super.page(request, dgm);//调用父类的分页逻辑
		rqp.setStatus(Constants.ROUTE_STATUS_OPEN);//只返回开通的线路
		Page page= routeInfoService.query(dgm, rqp);
		request.setAttribute("page", page);
		request.setAttribute("rqp", rqp);
		if(page.getRows().size()>0)
		 request.setAttribute("orgCity",((RouteInfoDomain)page.getRows().get(0)).getOrgCityCn());
		return "pages/front/route/route_list";
	}
}
